#include <iostream>
#include <Runtime/Window/Window.h>
#include <Runtime/Core/Core.h>
#include <Runtime/Graphics/Device/GraphicsDevice.h>
#include <Runtime/Graphics/Swapchain/Swapchain.h>
#include <Runtime/Graphics/Framebuffer/Framebuffer.h>
#include <Runtime/Graphics/Texture/TextureFormat.h>
#include <Runtime/Graphics/Texture/TextureView.h>
#include <Runtime/Graphics/Texture/Texture.h>
#include <Runtime/Graphics/Shader/ShaderReader.h>
#include <Runtime/Graphics/Shader/ShaderDesc.h>
#include <Runtime/Graphics/Shader/Shader.h>
#include <Runtime/Graphics/Sampler/SamplerDesc.h>
#include <Runtime/Graphics/Sampler/Sampler.h>
#include <Runtime/Graphics/Pipeline/Pipeline.h>
#include <Runtime/Graphics/Buffer/GraphicsBuffer.h>
#include <Runtime/Graphics/Command/CommandList.h>
#include <Runtime/Resources/ResourceImporter.h>
#include <Runtime/Resources/Mesh/Mesh.h>
#include <Runtime/Resources/Texture/TextureResource.h>
#include <Runtime/Graphics/GraphicsManager.h>
#include <Runtime/Application/ApplicationManager.h>
#include <Runtime/World/ObjectPipeline.h>
#include <Runtime/World/Camera.h>
#include <Runtime/Window/WindowManager.h>
#include <Runtime/World/GameObject.h>
#include <Runtime/World/CameraManager.h>
#include <Runtime/World/Light.h>

std::vector<std::shared_ptr<GameObject>> objects;

int main() {
	DEV_LOG(TE_INFO, "MAIN", "Window Start!");

	auto app = ApplicationManager::GetInstance().GetApplication();
	auto camera = CameraManager::GetInstance().GetCamera();
	auto pipeline = std::make_shared<ObjectPipeline>();
	auto light = LightManager::GetInstance().GetLight();

	auto commandList = app->GetCommandList();

	auto helmet = std::make_shared<Helmet>();
	objects.push_back(helmet);
	auto lantern = std::make_shared<Lantern>();
	objects.push_back(lantern);
		

	while (app->IsRunning()) {
		app->Run();
		CameraManager::GetInstance().Update();

		commandList->BindPipeline(pipeline->GetPipeline());

		for (auto& obj : objects) {
			obj->Update();

			commandList->BindVertexBuffer({ obj->mMesh->GetPositionBuffer(), obj->mMesh->GetTexCoordBuffer(), obj->mMesh->GetNormalBuffer() });
			commandList->BindIndexBuffer({ obj->mMesh->GetIndexBuffer() });

			commandList->UpdateDynamicBuffer(obj->mGraphicsBuffer, &obj->mModelMatrix, sizeof(CBData));
			commandList->BindResource({ obj->mBaseColor->GetTextureView(), obj->mEmissive->GetTextureView(), obj->mNormal->GetTextureView() },
				{ pipeline->GetSampler() }, { light->mBuffer }, ShaderStage::PixelShader);
			commandList->BindResource({}, {}, { obj->mGraphicsBuffer }, ShaderStage::VertexShader);

			commandList->DrawIndexed(obj->mMesh->GetIndices().size(), 0, 0);
		}

		
		//commandList->DrawIndexed(testMesh->GetIndices().size(), 0, 0);

		app->GetDevice()->ExecuteCommandLists({ app->GetCommandList()});
		app->GetDevice()->Present();
	}

	return 0;
}

